package com.group.attendance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;





public class AttendaceDAO {	

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		DataSource ds = null;
		
		public AttendaceDAO() {
			try {
				//	pooling 처리 : dbcp
				Context context = new InitialContext();
				ds = (DataSource)context.lookup("java:comp/env/jdbc/Oracle10g");
			} catch (Exception e) {
				System.out.println("연결 오류:" + e.getMessage());
				return;
			}
		}
	//출근 입력부분
		public ArrayList<AttendaceBean> getData(){
			ArrayList<AttendaceBean>list = new ArrayList<>();
			try{
				conn = ds.getConnection();
				String sql = "SELECT name,check_in,check_out,check_div FROM emp_inf";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				while(rs.next()){
					AttendaceBean bean = new AttendaceBean();
					
					
					bean.setNAME(rs.getString("NAME"));					
					bean.setCHECK_IN(rs.getString("CHECK_IN"));
					bean.setCHECK_OUT(rs.getString("CHECK_OUT"));
					bean.setCHECK_DIV(rs.getString("CHECK_DIV"));
					
					list.add(bean);
				}
			}catch(Exception e){
				System.out.println("getData 오류 : " + e.getMessage());
			}finally{
				try{				
					if(pstmt != null)pstmt.close();	
					if(conn != null)conn.close();
				}catch(Exception e){

				}
			}
			return list;
		}
		
		public boolean getAttendTime(String id){
			boolean b = false;
			String sql = "insert into EMP_INF(code_no,name,check_in,check_out,check_div) values(EMP_INF_NO.NEXTVAL,(SELECT NAME FROM MEMBER WHERE id='"+id+"') ,sysdate,sysdate,'조퇴')";
			try {
				conn = ds.getConnection();
				pstmt = conn.prepareStatement(sql);
				int count = pstmt.executeUpdate();
				if(count>0){
					b = true;
				}
			}catch(Exception e){
				System.out.println("getAttendTime 오류 : " + e.getMessage());
			}finally{
				try{				
					if(pstmt != null)pstmt.close();	
					if(conn != null)conn.close();
				}catch(Exception e){

				}
			}
			return b;
		}
		public boolean getOUTTime(){
			boolean b = false;
			String sql = "UPDATE EMP_INF SET CHECK_OUT = SYSDATE";
			try {
				conn = ds.getConnection();
				pstmt = conn.prepareStatement(sql);
				int count = pstmt.executeUpdate();
				if(count>0){
					b = true;
				}
			}catch(Exception e){
				System.out.println("getOUTTime 오류 : " + e.getMessage());
			}finally{
				try{				
					if(pstmt != null)pstmt.close();	
					if(conn != null)conn.close();
				}catch(Exception e){

				}
			}
			return b;
		}
	
}
